<?php

namespace Sayari\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Candidats
 *
 * @ORM\Table(name="candidats")
 * @ORM\Entity(repositoryClass="Sayari\UserBundle\Entity\CandidatsRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Candidats
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="firstname", type="string", length=255)
     */
    private $firstname;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="text")
     */
    private $address;

    /**
     * @var integer
     *
     * @ORM\Column(name="phone", type="integer")
     */
    private $phone;
    
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $cv;
    
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $photo;
    
    /**
     * @Assert\File(maxSize="6000000")
     */
    public $cv_file;
    
    /**
     * @Assert\File(maxSize="6000000")
     */
    public $photo_file;

    /**
     * @ORM\OneToOne(targetEntity="Sayari\UserBundle\Entity\Users", cascade={"persist"})
     */
    private $user;
    
    /**
     * @ORM\ManyToOne(targetEntity="Sayari\TalentissiaBundle\Entity\Regions")
     * @ORM\JoinColumn(nullable=false)
     */
    private $region;
    
    /**
     * @ORM\ManyToOne(targetEntity="Sayari\TalentissiaBundle\Entity\Specialites")
     * @ORM\JoinColumn(nullable=false)
     */
    private $specialite;
    
    /**
     * @ORM\ManyToOne(targetEntity="Sayari\TalentissiaBundle\Entity\Fonctions")
     * @ORM\JoinColumn(nullable=false)
     */
    private $fonction;
    
    /**
     * @ORM\ManyToOne(targetEntity="Sayari\TalentissiaBundle\Entity\NiveauEtude")
     * @ORM\JoinColumn(nullable=false)
     */
    private $niveau_etude;
    
    /**
     * @ORM\ManyToOne(targetEntity="Sayari\TalentissiaBundle\Entity\NiveauExperience")
     * @ORM\JoinColumn(nullable=false)
     */
    private $niveau_experience;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Clients
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set firstname
     *
     * @param string $firstname
     * @return Clients
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;
    
        return $this;
    }

    /**
     * Get firstname
     *
     * @return string 
     */
    public function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * Set address
     *
     * @param string $address
     * @return Clients
     */
    public function setAddress($address)
    {
        $this->address = $address;
    
        return $this;
    }

    /**
     * Get address
     *
     * @return string 
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * Set phone
     *
     * @param integer $phone
     * @return Clients
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;
    
        return $this;
    }

    /**
     * Get phone
     *
     * @return integer 
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Set user_id
     *
     * @param \Sayari\UserBundle\Entity\Users $userId
     * @return Clients
     */
    public function setUserId(\Sayari\UserBundle\Entity\Users $userId = null)
    {
        $this->user_id = $userId;
    
        return $this;
    }

    /**
     * Get user_id
     *
     * @return \Sayari\UserBundle\Entity\Users 
     */
    public function getUserId()
    {
        return $this->user_id;
    }

    /**
     * Set user
     *
     * @param \Sayari\UserBundle\Entity\Users $user
     * @return Clients
     */
    public function setUser(\Sayari\UserBundle\Entity\Users $user = null)
    {
        $this->user = $user;
    
        return $this;
    }

    /**
     * Get user
     *
     * @return \Sayari\UserBundle\Entity\Users 
     */
    public function getUser()
    {
        return $this->user;
    }
    
    public function __toString()
    {
        return $this->name.' '.$this->firstname;
    }
    

    /**
     * Set region
     *
     * @param \Sayari\TalentissiaBundle\Entity\Regions $region
     * @return Candidats
     */
    public function setRegion(\Sayari\TalentissiaBundle\Entity\Regions $region)
    {
        $this->region = $region;
    
        return $this;
    }

    /**
     * Get region
     *
     * @return \Sayari\TalentissiaBundle\Entity\Regions 
     */
    public function getRegion()
    {
        return $this->region;
    }

    /**
     * Set specialite
     *
     * @param \Sayari\TalentissiaBundle\Entity\Specialites $specialite
     * @return Candidats
     */
    public function setSpecialite(\Sayari\TalentissiaBundle\Entity\Specialites $specialite)
    {
        $this->specialite = $specialite;
    
        return $this;
    }

    /**
     * Get specialite
     *
     * @return \Sayari\TalentissiaBundle\Entity\Specialites 
     */
    public function getSpecialite()
    {
        return $this->specialite;
    }

    /**
     * Set fonction
     *
     * @param \Sayari\TalentissiaBundle\Entity\Fonctions $fonction
     * @return Candidats
     */
    public function setFonction(\Sayari\TalentissiaBundle\Entity\Fonctions $fonction)
    {
        $this->fonction = $fonction;
    
        return $this;
    }

    /**
     * Get fonction
     *
     * @return \Sayari\TalentissiaBundle\Entity\Fonctions 
     */
    public function getFonction()
    {
        return $this->fonction;
    }

    /**
     * Set niveau_etude
     *
     * @param \Sayari\TalentissiaBundle\Entity\NiveauEtude $niveauEtude
     * @return Candidats
     */
    public function setNiveauEtude(\Sayari\TalentissiaBundle\Entity\NiveauEtude $niveauEtude)
    {
        $this->niveau_etude = $niveauEtude;
    
        return $this;
    }

    /**
     * Get niveau_etude
     *
     * @return \Sayari\TalentissiaBundle\Entity\NiveauEtude 
     */
    public function getNiveauEtude()
    {
        return $this->niveau_etude;
    }

    /**
     * Set niveau_experience
     *
     * @param \Sayari\TalentissiaBundle\Entity\NiveauExperience $niveauExperience
     * @return Candidats
     */
    public function setNiveauExperience(\Sayari\TalentissiaBundle\Entity\NiveauExperience $niveauExperience)
    {
        $this->niveau_experience = $niveauExperience;
    
        return $this;
    }

    /**
     * Get niveau_experience
     *
     * @return \Sayari\TalentissiaBundle\Entity\NiveauExperience 
     */
    public function getNiveauExperience()
    {
        return $this->niveau_experience;
    }
    
    
    /*****************UPLOAD**************/
    public function getAbsoluteCV()
    {
        return null === $this->cv ? null : $this->getUploadRootDir().'/'.$this->cv;
    }

    public function getWebCV()
    {
        return null === $this->cv ? null : $this->getUploadDir().'/'.$this->cv;
    }
    
    public function getAbsolutePhoto()
    {
        return null === $this->photo ? null : $this->getUploadRootDir().'/'.$this->photo;
    }

    public function getWebPhoto()
    {
        return null === $this->photo ? null : $this->getUploadDir().'/'.$this->photo;
    }

    protected function getUploadRootDir()
    {
        // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
        // le document/image dans la vue.
        return 'uploads/documents';
    }
    
    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        $rand = sha1(uniqid(mt_rand(), true));
        if (null !== $this->cv_file) {
            // faites ce que vous voulez pour générer un nom unique
            $this->cv = 'cv_'.$rand.'.'.$this->cv_file->guessExtension();
        }
        
        if (null !== $this->photo_file) {
            // faites ce que vous voulez pour générer un nom unique
            $this->photo = 'photo_'.$rand.'.'.$this->photo_file->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->cv_file && null === $this->photo_file) {
            return;
        }

        // s'il y a une erreur lors du déplacement du fichier, une exception
        // va automatiquement être lancée par la méthode move(). Cela va empêcher
        // proprement l'entité d'être persistée dans la base de données si
        // erreur il y a
        $this->cv_file->move($this->getUploadRootDir(), $this->cv);
        $this->photo_file->move($this->getUploadRootDir(), $this->photo);

        unset($this->cv_file);
        unset($this->photo_file);
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsoluteCV()) {
            unlink($file);
        }
        if ($file = $this->getAbsolutePhoto()) {
            unlink($file);
        }
    }
}